//中文
let data_zh = ['你知道吗', '六月', '一日', '星期一', '是', '儿童节'];
//英语
let data_en = ['do you know', 'June', 'One day', 'Monday', 'Yes', 'Children\'s day'];


//翻译中文还是英文
let lang = 'zh';

//d_swap 交换
let d_swap = document.querySelector("#d_swap");
//d_if 输入文字
let d_if = document.querySelector("#d_if");
//d_that 翻译结果
let d_that = document.querySelector("#d_that");
//d_result 翻译
let d_result = document.querySelector("#d_result");

//btn_if 翻译
let btn_if = document.querySelectorAll("#btn_if button");
//btn_than 翻译
let btn_than = document.querySelectorAll("#btn_than button");


//点击交换按钮
function lan() {


    //切换语言
    if (lang == 'zh') {
        lang = 'en';
    } else {
        lang = 'zh';
    }
    //console.log(lang);

    for (let i = 0; i < btn_if.length; i++) {
        btn_if[i].classList.remove('btn-primary');
        btn_if[i].classList.remove('btn-light');
        btn_than[i].classList.remove('btn-primary');
        btn_than[i].classList.remove('btn-light');
    }
    switch (lang) {
        case 'zh':
            btn_if[0].classList.add('btn-primary');
            btn_if[1].classList.add('btn-light');
            btn_than[0].classList.add('btn-primary');
            btn_than[1].classList.add('btn-light');
            break;
        case 'en':
            btn_if[0].classList.add('btn-light');
            btn_if[1].classList.add('btn-primary');
            btn_than[0].classList.add('btn-light');
            btn_than[1].classList.add('btn-primary');
            break;
    }

}
d_swap.addEventListener("click", lan);


//点击翻译按钮
function res() {

    //遍历哪个数组
    let data;

    //检测数据库里有没有这个词汇
    let if_text = false;

    //中文词库有没有
    for (let i in data_zh) {
        if (d_if.value == data_zh[i]) {
            //如果有就是true
            if_text = true;
            //console.log(i);
            data = data_zh;

            //把英文赋给右侧
            d_that.value = data_en[i];
            //console.log(lang);
            if (lang == 'en') {
                lan()
            }
        }
    }

    //英文词库有没有
    for (let i in data_en) {
        if (d_if.value == data_en[i]) {
            //如果有就是true
            if_text = true;
            //console.log(i);
            data = data_en;

            //把中文赋给右侧
            d_that.value = data_zh[i];
            if (lang == 'zh') {
                lan()
            }
        }
    }

    //console.log(if_text);
    if (!if_text) {
        d_that.value = "";
        alert("无法翻译");

    }
}
d_result.addEventListener("click", res);
